/**
 * Candidate number: 35834
 */
package tv.starfu.sequencer.matrix;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.ControlBuilder;
import de.lessvoid.nifty.builder.EffectBuilder;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.examples.controls.common.CommonBuilders;
import de.lessvoid.nifty.screen.DefaultScreenController;
import de.lessvoid.nifty.screen.Screen;
import java.util.ArrayList;
import java.util.TreeMap;
import tv.starfu.sequencer.model.SequencerModel;

public class NiftySequencer {
    
    private static Screen screen;
    private static Screen clearScreen;
    
    private static CommonBuilders builders = new CommonBuilders();
    private TreeMap<String, ArrayList<Integer>> pattern;
    private static SequencerModel seqModel;
    
    public NiftySequencer(Nifty nifty, SequencerModel seqModel) {
        
        NiftySequencer.seqModel = seqModel;   
        
        // Create pattern matrix screen
        createPatternScreen(nifty);  
        
        
        
        nifty.subscribeAnnotations(seqModel);
        
        
    }
    
    
    public void start(Nifty nifty) {
        createPatternScreen(nifty);  
        nifty.gotoScreen("start");
        
    }
    
    public void clear(Nifty nifty) {
        niftyGameplay(nifty);
        nifty.gotoScreen("play");
    }
    
    
    private static Screen niftyGameplay(final Nifty nifty) {
        clearScreen = new ScreenBuilder("play") {{
            controller(new DefaultScreenController() {            });
        }}.build(nifty);
        
        return clearScreen;
    }
    
    
    private static Screen createPatternScreen(final Nifty nifty) {
        
        MatrixAtomControlDefinition.register(nifty);
        /* Initial Screen */
        // Must have a uniqueID
        screen = new ScreenBuilder("start") {{

            /* Provide ScreenController to handle events 
             * Maybe it can also call data / query model? */
            controller(new DefaultScreenController() {            });
            
                /* Background Layer */
                layer(new LayerBuilder("trillian_background") {
                    { childLayoutCenter();
                      backgroundImage("Interface/astrosky.jpg");    
                
                /* Container panel */
                panel(new PanelBuilder("trillian_container") {
                    { childLayoutVertical();
                      alignCenter();
                      width("100%");
                      height("80%");
                      backgroundColor("#aaf8");
                      
                      /* Set transition effects */
                      onStartScreenEffect(new EffectBuilder("fade") {
                          {
                              length(3000);
                              effectParameter("start", "#0");
                              effectParameter("end", "#f");
                          }
                    });
                             
                      /* Header Panel */
                     panel(new PanelBuilder("header") {
                         {
                            valignTop();
                            height("20%");
                            backgroundColor("#44f8");
                        }
                     });  
                     
                      /* Splash Panel */
                     panel(new PanelBuilder("splash") {
                         {
                           // valignTop();
                            height("20%");
                            backgroundColor("#cdf8");
                            childLayoutCenter();
                                                  
                              onStartScreenEffect(new EffectBuilder("gradient") {
                                  {
                                        effectValue("offset", "0%", "color", "#66666fff");
                                        effectValue("offset", "85%", "color", "#000f");
                                        effectValue("offset", "100%", "color", "#44444fff");
                                }
                            });
                        }
                     });    // End trillian splash

                     /* Main panel */
                     panel(new PanelBuilder("main") {{
                            backgroundColor("#fef2");
                            childLayoutCenter();
                            
                            /* Buttons */
                            panel(new PanelBuilder("trillian_activation_grid_buttons") {{
                                childLayoutVertical();
                                width("100%");
                                height("100%");
                                
/*  ------------------------------------------------------------------------------------------------------------------ */                                
                            /* Channel One */
/*  ------------------------------------------------------------------------------------------------------------------ */                                
                            panel(new PanelBuilder("channel_one") {{
                                childLayoutHorizontal();
                                width("100%");
                                height("20%");
                                String hSpace = "10px";

                                control(new MatrixAtomBuilder("mb01") {{
                                    setPhase(seqModel.getPhase(0, 0));
                                    setChannel("0");
                                    setStep("0");
                                    controller(new MatrixAtomController());  
                                }});
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb02") {{
                                    setPhase(seqModel.getPhase(0, 1));
                                    setChannel("0");
                                    setStep("1");
                                    controller(new MatrixAtomController());  
                                }});  
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb03") {{
                                    setPhase(seqModel.getPhase(0, 2));
                                    setChannel("0");
                                    setStep("2");
                                    controller(new MatrixAtomController()); 
                                }});                           
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb04") {{
                                    setPhase(seqModel.getPhase(0, 3));
                                    setChannel("0");
                                    setStep("3");
                                    controller(new MatrixAtomController()); 
                                }});
                                panel(builders.hspacer(hSpace));
                                panel(builders.hspacer(hSpace));
                                
                                /* §§§§§§§§§§§§ */
                                
                                control(new MatrixAtomBuilder("mb05") {{
                                    setPhase(seqModel.getPhase(0, 4));
                                    setChannel("0");
                                    setStep("4");
                                    controller(new MatrixAtomController()); 
                                }});
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb06") {{
                                    setPhase(seqModel.getPhase(0, 5));
                                    setChannel("0");
                                    setStep("5");
                                    controller(new MatrixAtomController()); 
                                }});  
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb07") {{
                                    setPhase(seqModel.getPhase(0, 6));
                                    setChannel("0");
                                    setStep("6");
                                    controller(new MatrixAtomController()); 
                                }});                           
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb08") {{
                                    setPhase(seqModel.getPhase(0, 7));
                                    setChannel("0");
                                    setStep("7");
                                    controller(new MatrixAtomController()); 
                                }});
                                panel(builders.hspacer(hSpace));
                                panel(builders.hspacer(hSpace));

                                }}); 
/*  ------------------------------------------------------------------------------------------------------------------ */                                
                            /* End Channel One */
/*  ------------------------------------------------------------------------------------------------------------------ */                             
                                
                        
/*  ------------------------------------------------------------------------------------------------------------------ */                                
                            /* Channel Two */
/*  ------------------------------------------------------------------------------------------------------------------ */                                
                            panel(new PanelBuilder("channel_two") {{
                                childLayoutHorizontal();
                                width("100%");
                                height("20%");
                                String hSpace = "10px";

                                control(new MatrixAtomBuilder("mb11") {{
                                    setPhase(seqModel.getPhase(1, 0));
                                    setChannel("1");
                                    setStep("0");
                                    controller(new MatrixAtomController());  
                                }});
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb12") {{
                                    setPhase(seqModel.getPhase(1, 1));
                                    setChannel("1");
                                    setStep("1");
                                    controller(new MatrixAtomController());  
                                }});  
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb13") {{
                                    setPhase(seqModel.getPhase(1, 2));
                                    setChannel("1");
                                    setStep("2");
                                    controller(new MatrixAtomController()); 
                                }});                           
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb14") {{
                                    setPhase(seqModel.getPhase(1, 3));
                                    setChannel("1");
                                    setStep("3");
                                    controller(new MatrixAtomController()); 
                                }});
                                panel(builders.hspacer(hSpace));
                                panel(builders.hspacer(hSpace));
                                
                                /* §§§§§§§§§§§§ */
                                
                                control(new MatrixAtomBuilder("mb15") {{
                                    setPhase(seqModel.getPhase(1, 4));
                                    setChannel("1");
                                    setStep("4");
                                    controller(new MatrixAtomController()); 
                                }});
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb16") {{
                                    setPhase(seqModel.getPhase(1, 5));
                                    setChannel("1");
                                    setStep("5");
                                    controller(new MatrixAtomController()); 
                                }});  
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb17") {{
                                    setPhase(seqModel.getPhase(1, 6));
                                    setChannel("1");
                                    setStep("6");
                                    controller(new MatrixAtomController()); 
                                }});                           
                                panel(builders.hspacer(hSpace));

                                control(new MatrixAtomBuilder("mb18") {{
                                    setPhase(seqModel.getPhase(0, 7));
                                    setChannel("1");
                                    setStep("7");
                                    controller(new MatrixAtomController()); 
                                }});
                                panel(builders.hspacer(hSpace));
                                panel(builders.hspacer(hSpace));

                                }}); 
/*  ------------------------------------------------------------------------------------------------------------------ */                                
                            /* End Channel Two */
/*  ------------------------------------------------------------------------------------------------------------------ */                             
                                
                        
                            }});     // End trillian activation grid buttons
                            
                        }});        // End trillian main
                     
                     
                }});    // End container panel
            }});        // End layer         
        }}.build(nifty);
        return screen;            
    }
    

}
